package com.zeedev.flyingsaucer.beers.activities.umbrella;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.zeedev.flyingsaucer.beers.activities.ActivitiesConstants;
import com.zeedev.flyingsaucer.beers.activities.home.HomeActivity;
import com.zeedev.flyingsaucer.beers.activities.login.LoginActivity;
import mobi.zeedev.flyingsaucer.common.CredentialsModule.interfaces.ICredentials;
import mobi.zeedev.flyingsaucer.common.WebLinkModule.interfaces.UfoClubClient;
import roboguice.activity.RoboActivity;
import roboguice.activity.event.OnActivityResultEvent;
import roboguice.activity.event.OnRestartEvent;
import roboguice.activity.event.OnResumeEvent;
import roboguice.activity.event.OnStartEvent;
import roboguice.event.Observes;
import roboguice.util.Ln;


/**
 * User: James
 * Date: 2/2/11
 * Time: 11:31 AM
 */
public class UmbrellaActivity extends RoboActivity {

    @Inject
    @Named("autoPreferenceCredentials")
    Provider<ICredentials> credentialsProvider;

    @Inject
    UfoClubClient client;

    public void doOnActivityResultEvent(@Observes OnActivityResultEvent event) {
        Ln.d("doOnActivityResultEvent: " + event.toString());

        if (event.getRequestCode() == ActivitiesConstants.VIEW_LOGIN_SCREEN) {
            Ln.d("return from viewscreen");

            if (event.getResultCode() == ActivitiesConstants.MINIMIZE_LOGIN_SCREEN) {
                Ln.d("return from login: minimize");

//                minimize();
                closeApp();


            } else if (event.getResultCode() == Activity.RESULT_OK) {

                Ln.d("return from login: attempt login");

//                attemptLoginProcess();
                closeApp();

            } else if (event.getResultCode() == Activity.RESULT_CANCELED) {

                Ln.d("return from login: RESULT_CANCELED");

//                minimize();
                closeApp();

            } else if (event.getResultCode() == ActivitiesConstants.LOGIN_PRESSED) {

                Ln.d("return from login: login pressed");

                attemptLoginProcess();

            }

        } else if (event.getRequestCode() == ActivitiesConstants.VIEW_HOME_SCREEN) {

            Ln.d("return from homescreen");

            if (event.getResultCode() == ActivitiesConstants.MINIMIZE_HOME_SCREEN) {
                Ln.d("return from homescreen: minimize");

//                minimize();
                closeApp();

            } else if (event.getResultCode() == ActivitiesConstants.LOGOUT_HOME_SCREEN) {

                Ln.d("return from homescreen: logout");

                doLogout();

            } else if (event.getResultCode() == Activity.RESULT_CANCELED) {

                Ln.d("return from homescreen: RESULT_CANCELED");

//                minimize();
                closeApp();


            } else if (event.getResultCode() == Activity.RESULT_OK) {

                Ln.d("return from homescreen: RESULT_OK");

//                minimize();
                closeApp();


            }


        }

    }

    private void closeApp() {
        finish();
    }

    public void doOnRestart(@Observes OnRestartEvent event) {
        Ln.d("doOnRestart: " + event != null ? event.toString() : "null");

    }

    public void doOnStart(@Observes OnStartEvent event) {
        Ln.d("doOnStart: " + event != null ? event.toString() : "null");

    }

    public void doOnResume(@Observes OnResumeEvent event) {
        Ln.d("doOnResume: " + event != null ? event.toString() : "null");


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

//        setProgressBarIndeterminateVisibility(true);

        Ln.i("Started Umbrella App.");

        //we always want to run this oncreate, because the app will always finish in other cases
        //i dont know what happens during a recreation event though!?

        init(savedInstanceState);

    }

    private void init(Bundle savedInstanceState) {
        final ICredentials credentials = credentialsProvider.get();

        if (savedInstanceState == null) {
            Ln.d("init: savedInstance == null");

            if (client.isLoggedIn()) {
                viewHome();
            } else if (credentials.getUfoAutoLogin()) {
                attemptLoginProcess();

            } else if (!credentials.getUfoAutoLogin()) {
                viewLogin();
            }


        } else {
            //this is a recreated instance
            //i dont know what to do here!?
            Ln.d("init: savedInstance != null");


        }

    }

    private void attemptLoginProcess() {
        final ICredentials credentials = credentialsProvider.get();
        if (credentials.isLoginAvailable()) {

            umbrellaDownloader.execute();

        } else {

            viewLogin();

        }

    }


    @Inject
    UmbrellaDownloader umbrellaDownloader;


    //Actions

    void viewLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivityForResult(intent, ActivitiesConstants.VIEW_LOGIN_SCREEN);
    }

    void viewHome() {
        Intent intent = new Intent(this, HomeActivity.class);
        startActivityForResult(intent, ActivitiesConstants.VIEW_HOME_SCREEN);
    }

    void doLogout() {
//        client.logout();
        viewLogin();
    }

    void minimize() {
        startActivity(new Intent().setAction(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
    }


}
